package script.dj;

import dbutils.DBSql;
import dbutils.DataScore;
import dbutils.SqlCreateUtils;
import httputils.OkHttpUtils;
import txtutils.TxtUtils;
import dm.jdbc.driver.DmdbNClob;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/7/8 11:21
 * @since JDK 1.8
 *
 * 内容操作脚本
 */
public class DJContentScript {

    public static void main(String[] args) throws Exception {
        dataRequest();
     //  sortContent();
       // deleteContent();
        //selectContentById(69447);
        //returnReplace("/uploadfiles/2014/11/201411271045234523.doc");
    }




    public static void returnReplace(String href){
        String tpl="<p style=\"text-align:left;\"><span style=\"line-height:1.5;\"><strong>相关附件：</strong><a href=\"%s\" target=\"_blank\">点击下载文件</a></span> </p><p>\t<br/></p>";
        String videotpl="<p style=\"height:0px\"><br/></p><p><video controls=\"\" src=\"%s\" style=\"max-width:750px\">no video</video></p><p><br/></p>";
        String content=String.format(tpl,href);
        String videContent=String.format(videotpl,"/u/cms/www/202106/29173359asxi.mp4");
        System.out.println(videContent);
        System.out.println(content);
    }

    /**
     * 请求数据添加
     */
    public static void dataRequest(){
        for (int i =3 ; i <54 ; i++) {
            String string=OkHttpUtils.builder().url("http://localhost:8080/cmsmanager/move/rcgzdj/1/"+i)
                    // 有参数的话添加参数，可多个
                    // 也可以添加多个
                    .addHeader("Content-Type", "application/json; charset=utf-8")
                    .addHeader("Connection","keep-alive")
                    .addHeader("JEECMS-Auth-Token","eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzeXN0ZW0iLCJjcmVhdGVkIjoxNjI3NTQ2MDI1MTA1LCJ1c2VyU291cmNlIjoiYWRtaW4iLCJleHAiOjE2Mjg0MTAwMjV9.ehEUncVHS5HF5y6IIdWwk3ouVpMcZIlKgJ2rmhUsH2GbLcwjqhjpvuSODXS4gi34guRPSKPgha1qexG05ZCJ9w")
                    .get()
                    // 可选择是同步请求还是异步请求
                    //.async();
                    .sync();
            System.out.println(string);
        }
    }

    /**
     * 查询内容的具体信息
     * @param id
     */
    public static void selectContentById(Integer id){
        DBSql dbSql=getDBSqlByDEV();
        Map<String, Object> stringObjectMap=dbSql.selectMapObjs(String.format("select * from jc_content where id='%s'",id));
        System.out.println(stringObjectMap);
        Map<String, Object> channelObjectMap=dbSql.selectMapObjs(String.format("select * from jc_channel where channel_id='%s'",stringObjectMap.get("channel_id")));
        Map<String, Object> contentObjectMap=dbSql.selectMapObjs(String.format("select * from jc_content_txt where content_id='%s' limit 1",id));
        String title=String.valueOf(stringObjectMap.get("title"));
        String channelName=String.valueOf(channelObjectMap.get("title"));
        String content=dbSql.ClobToString((DmdbNClob)contentObjectMap.get("attr_txt"));
        System.out.println(title);
        System.out.println(channelName);
       // System.out.println(content.replaceAll("br","\n"));
    }

    public static void sortPage(){
    }

    public static void sortContent() throws Exception{
        DBSql dbSql=getDBSqlByQA();
        List<Map<String, Object>> mapList=dbSql.selectMapObjList("select * from jc_content where deleted_flag='0' order by release_time ");
        List<Map<String, Object>> object=new ArrayList<>();
//       int n=0;
//        for (int i = 0; i < mapList.size() ; i++) {
//            object.add(mapList.get(i));
//            if(i%1000==0){
//
//               // n=i;
//               String sql=getUpdateSql(object,n);
//               n=i;
//                n++;
//               dbSql.sqlExecute(sql);
//               object.clear();
//            }
//        }
//
//
//         String sql2=getUpdateSql(object,n);
//            dbSql.sqlExecute(sql2);
        //  System.out.println(stringJoiner.toString());

        StringJoiner stringJoiner=new StringJoiner(";");
        for (int i = 0; i < mapList.size() ; i++) {
            Integer id=(Integer) mapList.get(i).get("id");
            String s=String.format("update jc_content set sort_num='%s'  where id='%s'",i,id);
            stringJoiner.add(s+"\n");
        }
        TxtUtils.writeAppendTxtFile(new File("C:\\Users\\xiong\\Desktop\\sql.txt"),stringJoiner.toString());

       // String sql2=getUpdateSql(object,n);
    //    dbSql.sqlExecute(sql2);
      //  System.out.println(stringJoiner.toString());
     //
        System.out.println("更新成功");
    }




    public static String getUpdateSql(List<Map<String, Object>> mapList,int num){
        StringJoiner stringJoiner=new StringJoiner(";");
        for (int i = 0; i < mapList.size() ; i++) {
            int num2=num+i;
            Integer id=(Integer) mapList.get(i).get("id");
            String s=String.format("update jc_content set sort_num='%s'  where id='%s'",num2,id);
            System.out.println(s);
            stringJoiner.add(s+"\n");
        }
        return stringJoiner.toString();
    }

    /**
     * 内容清除
     */
    public static void deleteContent(){
        DBSql dbSql=getDBSqlByQA();
        List<Map<String, Object>> mapList=dbSql.selectMapObjList("select * from jc_content where deleted_flag='1'");
        String deleteContentTxtSql=SqlCreateUtils.deleteListSql("jc_content_txt",mapList,"id","content_id");
        String deleteSql=SqlCreateUtils.deleteListSql("jc_content",mapList,"id");
        dbSql.sqlExecute(deleteContentTxtSql);
        dbSql.sqlExecute(deleteSql);
        System.out.println("删除成功");
    }


    public static DBSql getDBSqlByDEV(){
        Map<String, String> stringMap=DataScore.DEV_DJ();
        return new DBSql(stringMap);
    }

    public static DBSql getDBSqlByQA(){
        Map<String, String> stringMap= DataScore.QA_DJ();
        return new DBSql(stringMap);
    }
}
